package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.IotDeviceYkYtDit;
import com.econ.powercloud.ems.dal.mapper.IotDeviceYkYtDitMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author jwc
 * @since 2023/06/20
 */
@Repository
public class IotDeviceYkYtDitDAO extends ServiceImpl<IotDeviceYkYtDitMapper, IotDeviceYkYtDit> {

    public List<IotDeviceYkYtDit> selectByDeviceCodeAndDitAndRemoteType(List<String> deviceCodes, List<String> dits, Integer remoteType) {
        LambdaQueryWrapper<IotDeviceYkYtDit> wrapper = Wrappers.<IotDeviceYkYtDit>lambdaQuery()
                .eq(IotDeviceYkYtDit::getRemoteType, remoteType)
                .in(IotDeviceYkYtDit::getDeviceCode, deviceCodes)
                .in(IotDeviceYkYtDit::getDit, dits);
        return this.list(wrapper);
    }

    public Page<IotDeviceYkYtDit> selectPageBySearch(String deviceCode, Integer remoteType, String search, Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<IotDeviceYkYtDit> wrapper = Wrappers.<IotDeviceYkYtDit>lambdaQuery()
                .eq(IotDeviceYkYtDit::getDeviceCode, deviceCode)
                .eq(IotDeviceYkYtDit::getRemoteType,remoteType)
                .and(StringUtils.isNotBlank(search), item -> item.like(IotDeviceYkYtDit::getDit, search).or().like(IotDeviceYkYtDit::getDitName, search));
        Page<IotDeviceYkYtDit> page = new Page<>(pageNo, pageSize);
        return this.page(page, wrapper);
    }

    public List<IotDeviceYkYtDit> getByDeviceCode(List<String> deviceCodes) {
        LambdaQueryWrapper<IotDeviceYkYtDit> wrapper = Wrappers.<IotDeviceYkYtDit>lambdaQuery()
                .in(IotDeviceYkYtDit::getDeviceCode, deviceCodes);
        return this.list(wrapper);
    }

    public IotDeviceYkYtDit getByDeviceCodeAndDit(String deviceCode, String dit) {
        LambdaQueryWrapper<IotDeviceYkYtDit> wrapper = Wrappers.<IotDeviceYkYtDit>lambdaQuery()
                .eq(IotDeviceYkYtDit::getDeviceCode, deviceCode)
                .eq(IotDeviceYkYtDit::getDit,dit);
        return this.baseMapper.selectOne(wrapper);
    }
}
